package com.yanqu.road.dao.impl.activity.suoyao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.suoyao.config.SuoYaoTopRewardConfig;
import com.yanqu.road.utils.string.StringUtils;


import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SuoYaoTopRewardConfigDaoImpl extends TempDao {

    public static String getSuoYaoTopRewardConfigMapSql = "select * from t_s_activity_suoyao_top where activityId=?";

    public  Map<Integer, SuoYaoTopRewardConfig> getSuoYaoTopRewardConfigMap(int activityId) {
        Map<Integer, SuoYaoTopRewardConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSuoYaoTopRewardConfigMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SuoYaoTopRewardConfig suoYaoRewardConfig = new SuoYaoTopRewardConfig();
                suoYaoRewardConfig.setActivityId(rs.getInt("activityId"));
                suoYaoRewardConfig.setRewardId(rs.getInt("rewardId"));
                suoYaoRewardConfig.setRound(rs.getInt("round"));
                suoYaoRewardConfig.setReward(rs.getString("reward"));
                suoYaoRewardConfig.setPreview(rs.getString("preview"));
                suoYaoRewardConfig.setSelectReward(rs.getString("selectReward"));
                suoYaoRewardConfig.setSelectPreview(rs.getString("selectPreview"));
                //110357=600,30#110354=600,30#110005=6,50
                List<String> rewardList = new ArrayList<>();
                List<Integer> chooseTimeLimitList = new ArrayList<>();

                String reward = suoYaoRewardConfig.getReward();
                if (!reward.equals("0")) {
                    List<String> strings = StringUtils.stringToStringList(reward, "#");
                    for (String s : strings) {
                        List<String> goodAndTime = StringUtils.stringToStringList(s, ",");
                        rewardList.add(goodAndTime.get(0));
                        chooseTimeLimitList.add(StringUtils.string2intSafe(goodAndTime.get(1), 0));
                    }
                }

                suoYaoRewardConfig.setRewardList(rewardList);
                suoYaoRewardConfig.setRewardLimit(chooseTimeLimitList);

                // 5;110357=600|110354=100|110005=50
                String selectReward = suoYaoRewardConfig.getSelectReward();
                if (!selectReward.equals("0")) {
                    List<String> items = StringUtils.stringToStringList(selectReward, ";");
                    suoYaoRewardConfig.setSelectedTime(StringUtils.string2intSafe(items.get(0), 0));
                    suoYaoRewardConfig.setSelectedLimit(StringUtils.stringToStringList(items.get(1), "\\|"));
                }

                map.put(suoYaoRewardConfig.getRewardId(),suoYaoRewardConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
